//
//  HJDTuesdayTableViewController.m
//  网易
//
//  Created by 胡金东 on 16/5/10.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDTuesdayTableViewController.h"
#import "HJDZhuTableViewCell.h"

#import "AFNetworking.h"

@interface HJDTuesdayTableViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSArray * taskArr;

@property (nonatomic,strong)UIScrollView *scroll;

@property (nonatomic,strong) UITableView * tableView;



@end

@implementation HJDTuesdayTableViewController

-(NSArray *)taskArr
{
    if (_taskArr ==nil) {
        
        _taskArr = [NSArray array];
    }
    return _taskArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,HJDWidth - 20, 120)];
    self.scroll.delegate = self;
    self.scroll.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.scroll.showsVerticalScrollIndicator = YES;//垂直移动
    self.scroll.showsHorizontalScrollIndicator = NO;//水平移动
    //    上下滑动
    
    [self.view addSubview:self.scroll];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, HJDWidth-20, 120)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.scroll addSubview:self.tableView];
    //    去除多余的分割线
    self.tableView.tableFooterView=[[UIView alloc]init];

    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    NSString *phone = [defa objectForKey:@"phonenum"];
    NSString *pregnancy = [defa objectForKey:@"pregnanc"];
    
    NSString *url = @"/app/attention.do";
    NSString *path = [Hurl stringByAppendingString:url];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"]= phone;
    params[@"pregnancy"] = pregnancy;
    [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *info = (NSDictionary *)responseObject;
        NSArray *arr = info[@"resultMap"][@"result"];
        NSString *success = info[@"resultCode"];
        if ([success integerValue]==998) {
            self.taskArr = [NSArray arrayWithObjects:@"宝宝已经出生",@"充足的休息和均衡饮食，使体力尽快复原",@"摄取足够的营养来应付以母乳喂哺BB", nil];
        }else{
            

        if (arr !=nil && arr.count>=2) {

        NSString *notice = info[@"resultMap"][@"result"][1][@"notice"];
            NSArray *array = [[NSArray alloc]init];
            array = [notice componentsSeparatedByString:@";"];
//            NSLog(@"self-----%@",array);
            self.taskArr = @[array[0],array[1],array[2]];
        }else{
            self.taskArr = [NSArray arrayWithObjects:@"宝宝已经出生",@"充足的休息和均衡饮食，使体力尽快复原",@"摄取足够的营养来应付以母乳喂哺BB", nil];

        }
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"服务器升级，请稍后再试"];
//        NSLog(@"%@",error);
    }];
    
    

    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.taskArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    HJDZhuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HJDZhuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.shiXianLbl.text = [NSString stringWithFormat:@"任务%ld:%@",indexPath.row + 1,self.taskArr[indexPath.row]];

    CGSize shiXianSize = [ cell.shiXianLbl.text boundingRectWithSize:CGSizeMake(HJDWidth-39, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:cell.shiXianLbl.font,NSFontAttributeName, nil] context:nil].size;
    CGRect shiXianRect = cell.frame;
    shiXianRect.size.height = shiXianSize.height+15;
    cell.frame = shiXianRect;

//     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    UITableViewCell  *CellH = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    self.scroll.contentSize = CGSizeMake(HJDWidth - HJDHorizontalMargin, CellH.frame.size.height*3+50);
    return CellH.frame.size.height;
}


@end
